# SPDX-FileCopyrightText: 2024 German Aerospace Center (DLR.e.V.)
# SPDX-FileContributor: Ferdinand Rewicki <ferdinand.rewicki@dlr.de>
#
# SPDX-License-Identifier: Apache-2.0

"""Load EDEN ISS Anomaly data."""
import numpy as np
from aeon.datasets import load_from_tsfile


def load_anomalies_from_edeniss_ts_file(sensor, basepath="data/"):
    """Load EDEN ISS anomalous sequences from ts files.

    Parameters
    ----------
    sensor: str
        The sensor type
    basepath: str
        The path where the .ts files are located

    Returns
    -------
        X : np-list
            The anomalous sequences
        y : List[str]
            seuqence labels of the form <sensor>_<ts-identifier>_<start_index>
        dim_lookup : List[int]
            channel assignments of the anomalies
        idx_lookup : List[List[Tuple[int, int]]]
            anomaly indices (start, end) in the respective channel
    """
    X, y = load_from_tsfile(f"{basepath}/{sensor}.ts")

    # dim_lookup
    dims = list(np.unique([label.split("_")[1] for label in y]))
    dim_lookup = [dims.index(label.split("_")[1]) for label in y]

    # idx_lookup
    idx_lookup = [
        (int(l.split("_")[-1]), int(l.split("_")[-1]) + x.shape[1])
        for x, l in zip(X, y)
    ]

    return X, y, np.array(dim_lookup), np.array(idx_lookup)
